package cn.hsa.fqz.task.controller;

import cn.hsa.fqz.handler.base.BaseController;
import cn.hsa.fqz.task.controller.vo.TTaskMedcClueOffsiteVO;
import cn.hsa.fqz.task.enums.UserCategoryEnums;
import cn.hsa.fqz.task.service.TTaskMedcClueOffsiteService;
import cn.hsa.fqz.task.service.dto.LoginUserDTO;
import cn.hsa.fqz.task.service.dto.TTaskMedcClueOffsiteDTO;
import cn.hsa.fqz.task.service.dto.TaskQueryDTO;
import cn.hsa.fqz.util.UserRegionUtils;
import cn.hsa.hsaf.core.framework.context.HsafContextHolder;
import cn.hsa.hsaf.core.framework.util.CurrentUser;
import cn.hsa.hsaf.core.framework.util.PageResult;
import cn.hsa.hsaf.core.framework.web.WrapperResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author Administrator
 * @version 1.0
 * @date 2023-09-25 10:13
 */
@Slf4j
@Api(value = "异地协查管理", tags = "异地协查管理")
@RestController
@RequestMapping("/taskoffsite")
public class TTaskOffsiteController extends BaseController {

    @Autowired
    private TTaskMedcClueOffsiteService tTaskMedcClueOffsiteService;

    @ApiOperation(value = "异地协查列表", notes = "异地协查列表")
    @PostMapping("/list")
    public WrapperResponse<PageResult<TTaskMedcClueOffsiteDTO>> supplierList(@RequestBody TaskQueryDTO query) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        UserCategoryEnums enums = UserRegionUtils.getUserCateEnumByAscm(loginUser.getRegion());
        query.getCondition().setBelongTo(enums.toString());
        query.getCondition().setPrv(loginUser.getRegion());
        query.getCondition().setRecieverBy(loginUser.getAccount());
        PageResult<TTaskMedcClueOffsiteDTO> result = tTaskMedcClueOffsiteService.getPageByCondition(query);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "添加异地协查", notes = "添加异地协查")
    @PostMapping("/offsite/add")
    public WrapperResponse<Integer> addTaskOffsite(@RequestBody TTaskMedcClueOffsiteVO vo) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        TTaskMedcClueOffsiteDTO dto = new TTaskMedcClueOffsiteDTO();
        BeanUtils.copyProperties(vo,dto);
        dto.setSendBy(loginUser.getAccount());
        dto.setSendByName(loginUser.getUname());
        dto.setSendTime(new Date());
        Integer result = tTaskMedcClueOffsiteService.addClueOffsite(dto);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "删除异地协查", notes = "删除异地协查")
    @PostMapping("/offsite/del")
    public WrapperResponse<Integer> deleteTaskOffsite(@RequestParam("offsiteId")Integer offsiteId) {
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        Integer result = tTaskMedcClueOffsiteService.deleteClueOffsite(offsiteId);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "是否发起异地协查", notes = "是否发起异地协查")
    @GetMapping("/hasoffsite")
    public WrapperResponse<Boolean> isExsistTaskOffsite(@RequestParam("taskId")Integer taskId,@RequestParam("medcCode")String medcCode,@RequestParam("cityCode")String cityCode){
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO loginUser = super.getLoginUser(currentUser);
        Boolean flag = tTaskMedcClueOffsiteService.isExsitsOffsite(taskId, medcCode,cityCode,loginUser.getAccount());
        return WrapperResponse.success(flag);
    }
}
